using System.Data;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Data;
using Spread.Common.DataStore;

namespace Spread.Table.Features
{
    public partial class IntroTable : Spread.Common.Features.Sample.BaseSampleControl
    {
        private AccessDataSource _dataSource;

        #region ctor
        public IntroTable()
        {
            InitializeComponent();
            Name = "Introduction";

            _dataSource = new DataService().AccessDataSource;

            SetSheetProperties(fpSpread.ActiveSheet);
            AddSampleTable(fpSpread.ActiveSheet);
        }
        #endregion

        #region Private methods
        private void SetSheetProperties(SheetView sheet)
        {
            fpSpread.StatusBarVisible = true;
            sheet.DataAutoSizeColumns = true;
            sheet.SetMultipleRowHeights(0, fpSpread.ActiveSheet.Rows.Count, 20);
        }

        private void AddSampleTable(SheetView sheet)
        {
            DataTable dataTable = _dataSource.GetDataFromTable("Nwind.mdb", "Products").Tables[0];
            SpreadDataBindingAdapter adapter = new SpreadDataBindingAdapter();
            adapter.Spread = fpSpread;
            adapter.SheetName = "Sheet1";
            adapter.DataSource = dataTable;
            adapter.MapperInfo = new MapperInfo(2, 1, 30, dataTable.Columns.Count);
            adapter.FillSpreadDataByDataSource();

            int col = 1;
            foreach (DataColumn column in dataTable.Columns)
                sheet.Cells[1, col++].Text = (column.Caption);

            TableView table = sheet.AddTable("Table1", 1, 1, 31, dataTable.Columns.Count,
                TableStyle.TableStyleMedium2);

            for (int j = 1; j < col; ++j)
                sheet.Columns[j].Width = sheet.Columns[j].GetPreferredWidth();

            table.HeaderRowVisible = true;
            table.FirstColumn = true;
            table.LastColumn = false;
            table.TotalRowVisible = true;
        }
        #endregion
    }
}


//
//IntroTable.Designer.cs
//


namespace Spread.Table.Features
{
    partial class IntroTable
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // TableIntro
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "TableIntro";
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
    }
}
